package com.xx.wars.node;

import javax.microedition.khronos.opengles.GL10;

import com.xx.framework.gl.Camera2D;
import com.xx.framework.gl.SpriteBatcher;
import com.xx.framework.gl.TextureRegion;
import com.xx.framework.impl.GLGraphics;
import com.xx.framework.math.Rectangle;
import com.xx.wars.screen.WorldRenderer;
import com.xx.wars.util.Assets;

public class CircularNode extends SpriteNode {

	private final int MINI_WIDTH = 5;
	

	public CircularNode(float x, float y, float width, float height) {
		super(x, y, width, height);
	}

	public CircularNode(String name, float x, float y, NodeEnum powerType,
			float hp, float maxHP, int recoverSpeed, int canAttackDist,
			float aiAttackTimer) {
		super(name, x, y, 64, 64, powerType, hp, maxHP, recoverSpeed,
				canAttackDist, aiAttackTimer);
	}

	public CircularNode(String name, float x, float y, int level,
			NodeEnum powerType, float hp, float maxHP, int recoverSpeed,
			int canAttackDist, float aiAttackTimer) {
		super(name, x, y, (int) (Math.pow(2, level + 6)), (int) (Math.pow(2,
				level + 6)), powerType, hp, maxHP, recoverSpeed, canAttackDist,
				aiAttackTimer);
		this.level = level;
		changePower = true;
	}

	public CircularNode(String name, float x, float y, NodeEnum powerType,
			float hp, float maxHP, int recoverHP, int canAttackDist,
			float aiAttackTimer, int defenseLevel, int maxHPLevel,
			int recoverHPLevel) {
		super(name, x, y, 64, 64, powerType, hp, maxHP, recoverHP,
				canAttackDist, aiAttackTimer, defenseLevel, maxHPLevel,
				recoverHPLevel);
	}

	public CircularNode(String name, float x, float y, int level,
			NodeEnum powerType, float hp, float maxHP, int recoverHP,
			int canAttackDist, float aiAttackTimer, int defenseLevel,
			int maxHPLevel, int recoverHPLevel) {
		super(name, x, y, (int) (Math.pow(2, level + 6)), (int) (Math.pow(2,
				level + 6)), powerType, hp, maxHP, recoverHP, canAttackDist,
				aiAttackTimer, defenseLevel, maxHPLevel, recoverHPLevel);
		this.level = level;
		changePower = true;
	}

	@Override
	public void initTexture() {
		if (textureRegion == null || changePower) {
			String path = "64x64";
			if (level == 0) {
				path = "64x64";
			} else if (level == 1) {
				path = "128x128";
			} else if (level == 2) {
				path = "256x256";
			}

			if (powerType == NodeEnum.player) {
				textureRegion = Assets.createTextureRegion("blue_circle_"
						+ path + ".png");
			} else if (powerType == NodeEnum.notPlayer) {
				textureRegion = Assets.createTextureRegion("white_circle_"
						+ path + ".png");
			} else if (powerType == NodeEnum.badPlayer) {
				textureRegion = Assets.createTextureRegion("black_circle_"
						+ path + ".png");
			} else if (powerType == NodeEnum.badPlayer2) {
				textureRegion = Assets.createTextureRegion("orange_circle_"
						+ path + ".png");
			} else if (powerType == NodeEnum.boss1) {
				textureRegion = Assets.createTextureRegion("yellow_circle_"
						+ path + ".png");
			}
			changePower = false;
		}
	}

	@Override
	public void update(float deltaTime) {
		super.update(deltaTime);
	}

	@Override
	public void present(SpriteBatcher batcher, Camera2D cam, GL10 gl) {
		super.present(batcher, cam, gl);

		int dis = 0;
		if (maxHPLevel > 0) {
			if (level == 0) {
				dis = -10;
			} else if (level == 1) {
				dis = -40;
			} else if (level == 2) {
				dis = -80;
			}
			int px = (int) (Math.cos((addAttackAngle * 2) * Math.PI / 360) * (bounds.width + dis));
			int py = (int) (Math.sin((addAttackAngle * 2) * Math.PI / 360) * (bounds.height + dis));
			batcher.beginBatch(textureRegion.getTexture());
			batcher.drawSprite(cam.position.x + position.x + px, cam.position.y
					+ position.y + py, maxHPLevel * MINI_WIDTH, maxHPLevel
					* MINI_WIDTH, addAttackAngle, textureRegion);
			// batcher.drawSprite(
			// cam.position.x + position.x + px,
			// cam.position.y + position.y + py,
			// 10,
			// 10,
			// addAttackAngle,
			// textureRegion);
			batcher.endBatch();
		}
		if (recoverHPLevel > 0) {
			if (level == 0) {
				dis = 20;
			} else if (level == 1) {
				dis = 0;
			} else if (level == 2) {
				dis = -30;
			}
			int px = (int) (Math.cos((addMaxHPAngle * 2) * Math.PI / 360) * (bounds.width + dis));
			int py = (int) (Math.sin((addMaxHPAngle * 2) * Math.PI / 360) * (bounds.height + dis));
			batcher.beginBatch(textureRegion.getTexture());
			batcher.drawSprite(cam.position.x + position.x + px, cam.position.y
					+ position.y + py, recoverHPLevel * MINI_WIDTH,
					recoverHPLevel * MINI_WIDTH, addMaxHPAngle, textureRegion);
			// batcher.drawSprite(
			// cam.position.x + position.x + px ,
			// cam.position.y + position.y + py,
			// 10,
			// 10,
			// addMaxHPAngle,
			// textureRegion);
			batcher.endBatch();
		}

//		if (defenseLevel > 0) {
//			if (level == 0) {
//				dis = 50;
//			} else if (level == 1) {
//				dis = 40;
//			} else if (level == 2) {
//				dis = 20;
//			}
//			int px = (int) (Math.cos((addRecoverAngle * 2) * Math.PI / 360) * (bounds.width + dis));
//			int py = (int) (Math.sin((addRecoverAngle * 2) * Math.PI / 360) * (bounds.height + dis));
//			batcher.beginBatch(textureRegion.getTexture());
//			batcher.drawSprite(cam.position.x + position.x + px, cam.position.y
//					+ position.y + py, defenseLevel * MINI_WIDTH, defenseLevel
//					* MINI_WIDTH, addRecoverAngle, textureRegion);
//			batcher.endBatch();
//			
//		}
	}

	@Override
	public TextureRegion getTextureRegion() {
		return textureRegion;
	}

	public Rectangle getBounds() {
		float halfWidth = bounds.width / 2;
		float halfHeight = bounds.height / 2;
		float x1 = position.x - halfWidth;
		float y1 = position.y - halfHeight;
		return new Rectangle(x1, y1, bounds.width, bounds.height);
	}

	@Override
	public void drawSelectedSphere(GLGraphics glGraphics, Camera2D guiCam,
			SpriteBatcher batcher, WorldRenderer renderer) {
		GL10 gl = glGraphics.getGL();
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		gl.glColor4f(0, 0, 0, 0.1f);
		batcher.beginBatch(selectedSphereTextureRegion.getTexture());
		batcher.drawSprite(renderer.cam.position.x + position.x,
				renderer.cam.position.y + position.y, canAttackDist * 2,
				canAttackDist * 2, selectedSphereTextureRegion);
		batcher.endBatch();
		gl.glColor4f(1, 1, 1, 1);
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisable(GL10.GL_BLEND);
	}

	@Override
	public Object clone() {
//		return new CircularNode(name, position.x, position.y, level, powerType,
//				hp, maxHP, recoverHP, canAttackDist, aiAttackTimer, maxHPLevel,
//				recoverHPLevel, defenseLevel);
		return new CircularNode(name, position.x, position.y, level, powerType,
				hp, maxHP, recoverHP, canAttackDist, aiAttackTimer, maxHPLevel,
				recoverHPLevel, 0);
	}
}
